package com.ruan.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruan.common.Code;
import com.ruan.common.Result;
import com.ruan.exception.BusinessException;
import com.ruan.mapper.DepartLocationMapper;
import com.ruan.mapper.DepartmentMapper;
import com.ruan.mapper.EmployeeLeaveMapper;
import com.ruan.mapper.EmployeeMapper;
import com.ruan.pojo.*;
import com.ruan.pojoExt.EmployeeExt;
import com.ruan.service.*;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;

/**
 * 用户
 */
@RestController
@CrossOrigin
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private CommonUserService commonUserService;

    @Autowired
    private DepartLocationService departLocationService;

    @Autowired
    private DepartLocationMapper departLocationMapper;

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private DepartmentMapper departmentMapper;

    @Autowired
    PositionService positionService;


    //管理员登录
    @PostMapping("/login/admin")
    public Result adminLogin(@RequestBody Admin admin){
        return adminService.adminLogin(admin);
    }


    //员工登录
    @PostMapping("/login/employee")
    public Result employeeLogin(@RequestBody CommonUser commonUser){
        return commonUserService.employeeLogin(commonUser);
    }


   //员工注册
    @PostMapping("/register")
    public Result employeeRegister(@RequestBody CommonUser commonUser){
        return commonUserService.register(commonUser);
    }


    //根据手机号查询员工
    @GetMapping("/selectByPhone")
    public Result selectByPhone(String phone){
        return commonUserService.selectByPhone(phone);
    }

    //注册完成后，初次登陆，填写信息
    @PostMapping("/addUser")
    public Result addUser(@RequestBody Employee employee, HttpServletRequest request){
        try {
            commonUserService.addUser(employee, request);
            return new Result(Code.SAVE_OK,"提交成功，请等待审核通过后登录！");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(Code.SAVE_ERR,"提交失败！该邮箱已注册");
        }
    }


    //通过手机号查询员工的姓名部门和手机号，返回到个人页面页面
    @GetMapping("/getUserBasicInfo")
    public Result getUserBasicInfo(String phone) {
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getPhone,phone);
        Employee employee = employeeMapper.selectOne(queryWrapper);
        EmployeeExt employeeExt = new EmployeeExt();
        //属性迁移
        BeanUtils.copyProperties(employee, employeeExt);
        System.out.println("属性迁移完了吗："+employeeExt);
        Department department = departmentService.getById(employee.getDepartmentId());
        employeeExt.setDepartmentName(department.getName());
        Position position = positionService.getById(employee.getPositionId());
        if(position != null) {
            employeeExt.setPosition(position.getPname());
        }
        LambdaQueryWrapper<DepartLocation> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(DepartLocation::getDepartmentId,department.getId());
        DepartLocation departLocation = departLocationMapper.selectOne(queryWrapper1);
        if(departLocation!=null){
            employeeExt.setLatitude(departLocation.getLatitude());
            employeeExt.setLongitude(departLocation.getLongitude());
        }
        if (employee!=null){
            return new Result(Code.SELECT_OK,employeeExt);
//            return employee;
        }else {
            return new Result(Code.SELECT_ERR,"信息初始化失败！");
        }
    }



    //通过手机号查询员工信息
    @GetMapping("/selectUserInfo")
    public Result selectUserInfo(String phone) {
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getPhone,phone);
        Employee employee = employeeMapper.selectOne(queryWrapper);
        EmployeeExt employeeExt = new EmployeeExt();

        BeanUtils.copyProperties(employee,employeeExt);
        System.out.println("属性搬完了吗："+employeeExt);
        Long departId = employee.getDepartmentId();

        Department department = departmentService.getById(departId);
        if(department != null) {
            employeeExt.setDepartmentName(department.getName());
        }
        Position position = positionService.getById(employee.getPositionId());
        if(position != null) {
            employeeExt.setPosition(position.getPname());
        }
        if (employee!=null){
            return new Result(Code.SELECT_OK,employeeExt);
        }
        else {
            return new Result(Code.SELECT_OK,"信息初始化失败！");
        }
    }

    //判断该员工账号是否可用
    @GetMapping("/judgeStatus/{userId}")
    public Result judgeStatus(@PathVariable Integer userId){
        return commonUserService.judgeStatus(userId);
    }

    @GetMapping("/selectUsername")
    public Result selectUsername(String username){

        return commonUserService.selectUsername(username);
    }


    @GetMapping("/logout")
    public Result logout(String username, HttpServletRequest request){
        try {
            if (username.equals("admin")){
                request.getSession().removeAttribute("admin");
                System.out.println("logout管理员的Session清空了？");
                return new Result(Code.LOGOUT_OK,"退出登录成功！");
            }else {
                request.getSession().removeAttribute("employee");
                System.out.println("logout普通员工的Session清空了？");
                return new Result(Code.LOGOUT_OK,"退出登录成功！");
            }
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(Code.LOGIN_ERR,"退出登录失败！");
        }
    }
}
